import 'package:flutter/material.dart';
import 'view_model.dart';
import 'api.dart';
import 'dart:async';
import 'package:scoped_model/scoped_model.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'films_page.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage>
    with SingleTickerProviderStateMixin {
  MainPageViewModel mainPageViewModel;
  TabController tabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    mainPageViewModel = MainPageViewModel(api: Swapi());
    tabController = TabController(vsync: this, length: 3);
    fetchData();
  }

  Future fetchData() async {
    await mainPageViewModel.fetchFilms();
    await mainPageViewModel.fetchCharacters();
    await mainPageViewModel.fetchPlanets();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          "Scoped_model Demo",
          style: TextStyle(
            fontFamily: 'Distant Galaxy',
          ),
        ),
        bottom: TabBar(
          controller: tabController,
          indicatorColor: Colors.white,
          indicatorWeight: 3.0,
          tabs: <Widget>[
            Tab(icon: Icon(FontAwesomeIcons.film)),
            Tab(icon: Icon(FontAwesomeIcons.users)),
            Tab(icon: Icon(FontAwesomeIcons.globeAmericas))
          ],
        ),
      ),
      body: ScopedModel<MainPageViewModel>(
        model: mainPageViewModel,
        child: TabBarView(
          controller: tabController,
          children: <Widget>[
            FilmsPanel(),
            FilmsPanel(),
            FilmsPanel(),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    tabController?.dispose();
    super.dispose();
  }
}
